using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Windows.Shapes;


namespace AutoUpdate
{
    public delegate void DelegateInvoker();

    public partial class Window1 : System.Windows.Window
    {

        #region Constructor

        public Window1()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window1_Loaded);

            close.Click += new RoutedEventHandler(close_Click);
            btnCancel.Click += new RoutedEventHandler(close_Click);
            borHeader.MouseLeftButtonDown += new MouseButtonEventHandler(borHeader_MouseLeftButtonDown);
        }

        #endregion

        #region Header bar management

        void borHeader_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        void close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Window1_Loaded

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncCheckUpdate));
        }

        #endregion

        #region CheckUpdate

        public void AsyncCheckUpdate(object state)
        {
            AutoUpdateResult result = AutoUpdateManager.CheckAutoUpdate();

            Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                (DelegateInvoker)delegate { CheckUpdate(result); });
        }

        public void CheckUpdate(AutoUpdateResult result)
        {
            btnProxy.IsEnabled = false;

            //---- Error
            if (!result.IsOk)
            {
                lblProgress.Text = result.Message;
                btnProxy.IsEnabled = true;
                btnCancel.IsEnabled = true;
                return;
            }

            //---- No new version
            if (!result.NewVersionExists)
            {
                lblProgress.Text = "Most recent version is installed : " + result.Version;
                btnCancel.Content = "Close";
                return;
            }

            //---- Force download
            if (result.ForceDownload)
            {
                lblProgress.Text = "A new version exist : " + result.Version;
                txtHyperlinkViewOn.Visibility = Visibility.Visible;
                btnProxy.IsEnabled = true;
                btnCancel.IsEnabled = true;
                return;
            }

            //---- Install the new version
            lblProgress.Text = "A new version exist : " + result.Version;

            MessageBoxResult msgResult = MessageBox.Show(this, "Do you want to download and install it now ?", "Automatic update ?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);

            if (msgResult == MessageBoxResult.Yes)
            {
                //---- Download the new files
                AutoUpdateManager.DownloadUpdate();

                //---- Install the new version, can register some COM component
                // -> must be done NOW !
                AutoUpdateManager.Update();

                lblProgress.Text = "Your version of ViewOn.tv is updated";
            }
            else
                this.Close();
        }

        #endregion

        #region Proxy Button

        //private void proxyButton_Click(object sender, EventArgs e)
        //{
        //    ProxyDialog dialog = new ProxyDialog();
        //    dialog.ShowDialog(this);
        //    if (dialog.IsOk)
        //    {
        //        AutoUpdateManager.UseProxy = dialog.UseProxy;
        //        AutoUpdateManager.ProxyAddress = dialog.Address;
        //        AutoUpdateManager.ProxyPort = dialog.Port;

        //        // Restart checking
        //        btnProxy.Enabled = false;
        //        lblProgress.Text = "";
        //        CheckUpdate();
        //    }
        //}

        #endregion

        void HandleRequestNavigate(object sender, RoutedEventArgs e)
        {
            string navigateUri = hlViewOn.NavigateUri.ToString();
            Process.Start(new ProcessStartInfo(navigateUri));
            e.Handled = true;
        }

    }
}